package com.ht.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ht.mapper.SysPermissionMapperCustom;
import com.ht.mapper.SysUserMapper;
import com.ht.po.SysPermission;
import com.ht.po.SysUser;
import com.ht.po.SysUserExample;
import com.ht.service.ISysUserService;

@Service
public class SysUserServiceImpl implements ISysUserService {

	@Autowired
	private SysUserMapper sysUserMapper;
	
	@Autowired
	private SysPermissionMapperCustom sysPermissionMapperCustom;
	
	// 通过用户账号判断用户是否存在
	public SysUser getUserByuserCode(String userCode) {
		
		SysUserExample sysUserExample = new SysUserExample();
		
		SysUserExample.Criteria cr = sysUserExample.createCriteria();
		
		cr.andUsercodeEqualTo(userCode);
		
		List<SysUser> selectByExample = sysUserMapper.selectByExample(sysUserExample);

		if(selectByExample != null && selectByExample.size() == 1) {
			SysUser user = selectByExample.get(0);
			return user;
		}
		return null;
	}
	
	// 新增用户
	public void registerUser(SysUser user) {
		user.setSalt("ssm" + new Date() + user.getUsercode());
		user.setLocked(0);
		user.setCreateAt(new Date());
		user.setCreateBy(0);
		user.setDelFlag(0);
		sysUserMapper.insert(user);
	}
	
	// 查询用户菜单
	public List<SysPermission> listMenu(Integer id) {
		return sysPermissionMapperCustom.listMenu(id);
	}
	
	// 查询用户权限
	public List<SysPermission> listPermission(Integer id) {
		return sysPermissionMapperCustom.listPermission(id);
	}
}
